package com.wang;

import com.wang.service.IDemoService;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericService;

import java.io.IOException;
import java.nio.channels.Channel;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

public class AppTestAsync {
    public static void main(String[] args) throws Exception{
        //创建服务应用对象实例
        ReferenceConfig<IDemoService> referenceConfig = new ReferenceConfig<>();
        //设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("dubbo-consumer"));
        //设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("nacos://127.0.0.1:8848"));
        //设置服务接口和超时时间
        referenceConfig.setInterface(IDemoService.class);
        referenceConfig.setTimeout(10000);
        //设置负载策略和集群容错策略
        //referenceConfig.setLoadbalance("myLoadBalance");
        //referenceConfig.setCluster("myBroadcast");
        //设置服务的版本号和分组
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dev");
        //设置为异步调用
        referenceConfig.setAsync(true);
        //引用服务
        IDemoService demoService = referenceConfig.get();
        RpcContext.getContext().setAttachment("company","xiaomi");
        String msg = demoService.getMsg("dev");
        System.out.println(msg);
        CompletableFuture<String> future = RpcContext.getContext().getCompletableFuture();//.getFuture();
        System.out.println(future.get());

    }
}
